/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.controller;

import static com.googlecode.duplicatedetector.i18n.Messages._;

import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.SwingWorker.StateValue;

import com.googlecode.duplicatedetector.i18n.Keys;
import com.googlecode.duplicatedetector.model.DuplicateFinder;
import com.googlecode.duplicatedetector.util.FileUtils;
import com.googlecode.duplicatedetector.view.ResultsPanel;

/**
 * Updates the search progress and shows the results when finished.
 * 
 * @author Marco Biscaro
 */
public class ProgressUpdater implements PropertyChangeListener, Keys {

	private JProgressBar progressBar;
	private JTextArea detailsTextArea;
	private DuplicateFinder finder;

	public ProgressUpdater(DuplicateFinder finder, JProgressBar progressBar,
			JTextArea detailsTextArea) {
		this.finder = finder;
		this.progressBar = progressBar;
		this.detailsTextArea = detailsTextArea;
		finder.addPropertyChangeListener(this);
		progressBar.setIndeterminate(true);
		progressBar.setString(_(SEARCHING_DIRS));
	}

	@Override
	public void propertyChange(PropertyChangeEvent e) {
		if ("progress".equals(e.getPropertyName())) {
			progressBar.setIndeterminate(false);
			progressBar.setString(null);
			progressBar.setValue(finder.getProgress());
			return;
		}
		if ("scan".equals(e.getPropertyName())) {
			if (e.getNewValue() == StateValue.STARTED) {
				detailsTextArea.append(_(SEARCHING_DIRS));
				detailsTextArea.append("\n");
			} else if (e.getNewValue() == StateValue.DONE) {
				detailsTextArea.append(_(FILES_FOUND));
				detailsTextArea.append(finder.getTotalFiles() + "");
				detailsTextArea.append("\n");
			}
			return;
		}
		if ("filter".equals(e.getPropertyName())) {
			if (e.getNewValue() == StateValue.STARTED) {
				detailsTextArea.append(_(FILES_WITH_SAME_SIZE));
				detailsTextArea.append(finder.getFilesWithSameSizeCount() + "");
				detailsTextArea.append("\n");
				detailsTextArea.append(_(VERIFYING_FILES));
				detailsTextArea.append("\n");
			} else if (e.getNewValue() == StateValue.DONE) {
				detailsTextArea.append(_(FILES_WITH_SAME_BYTES));
				detailsTextArea.append(finder.getPossibleDuplicateFiles() + "");
				detailsTextArea.append("\n");
			}
			return;
		}
		if ("hash".equals(e.getPropertyName())) {
			if (e.getNewValue() == StateValue.STARTED) {
				detailsTextArea.append(_(CALCULATING_HASHES));
				detailsTextArea.append("\n");
			} else if (e.getNewValue() == StateValue.DONE) {
				detailsTextArea.append(_(DUPLICATED_FILES));
				detailsTextArea.append(finder.getDuplicates() + "");
				detailsTextArea.append("\n");
				detailsTextArea.append(_(DONE));
				detailsTextArea.append("\n");
				detailsTextArea.append(_(WASTED_SPACE));
				detailsTextArea.append(FileUtils.getLength(finder
						.getWastedSpace()));
				detailsTextArea.append("\n");
				detailsTextArea.append(_(ELAPSED_TIME));
				detailsTextArea.append(finder.getElapsedTime() + " ms");
				detailsTextArea.append("\n");
				if (finder.getDuplicates() > 0) {
					try {
						showResults(finder.get());
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					} catch (ExecutionException e1) {
						e1.printStackTrace();
					}
				}
			}
		}
	}

	private void showResults(Map<String, Set<File>> map) {
		Window parent = SwingUtilities.getWindowAncestor(progressBar);
		JDialog dialog = new JDialog(parent);
		dialog.setModal(true);
		dialog.setTitle(_(RESULTS));
		dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		dialog.setContentPane(new ResultsPanel(map));
		dialog.pack();
		dialog.setMinimumSize(dialog.getSize());
		dialog.setLocationRelativeTo(parent);
		dialog.setVisible(true);
	}

}
